//
//  CS_Page_TabbarContainerView.m
//  Pods
//
//  Created by apple on 2017/8/30.
//
//

#import "CS_Page_TabbarContainerView.h"

//顶部滚动条高度
#define HEIGHT_SCROLL_BAR 40.0
#define HEGITH_SCROLL_LINEW 2.5

@interface CS_Page_TabbarContainerView()<UIScrollViewDelegate>

@property (nonatomic, assign) CGFloat needHeight;

@property (nonatomic, strong) NSMutableArray *scrollViews;

@property (nonatomic, strong) UIScrollView *topScrollView;

@property (nonatomic, strong) UIScrollView *bottomScrollView;

@property (nonatomic, strong) NSMutableArray *tabButtons;

//底部滑动条
@property (nonatomic, strong) UIView *bottomLinewView;

@property (nonatomic, strong) UIButton *selectedButton;

@property (nonatomic, assign) CGFloat previousOffsetX;

@property (nonatomic, assign) BOOL isTopAnimating;

@property (nonatomic, copy) OnTabIndexChanged onTabIndexChangedHandler;

@end

@implementation CS_Page_TabbarContainerView

+(id)page_TabbarContainerView:(NSArray *)tabTitles needHeight:(CGFloat)needHeight{
    
    CS_Page_TabbarContainerView *page_TabbarContainerView = [CS_Page_TabbarContainerView new];
    page_TabbarContainerView.tabTitles = tabTitles;
    page_TabbarContainerView.needHeight = needHeight;
    
    return page_TabbarContainerView;
    
}

+(id)page_TabbarContainerView:(NSArray *)tabTitles needHeight:(CGFloat)needHeight onTabIndexChangedHandler:(OnTabIndexChanged)onTabIndexChangedHandler{
    
    CS_Page_TabbarContainerView *page_TabbarContainerView = [CS_Page_TabbarContainerView new];
    page_TabbarContainerView.tabTitles = tabTitles;
    page_TabbarContainerView.needHeight = needHeight;
    page_TabbarContainerView.onTabIndexChangedHandler = onTabIndexChangedHandler;
    
    return page_TabbarContainerView;
}

-(CGFloat)layoutView:(UIView *)superView primaryY:(CGFloat)primaryY{
    
    CGFloat localPrimaryY = 0;
    
    if (self.tabTitles.count == 1
        &&[self isNullOrEmpty:[self.tabTitles objectAtIndex:0]]) {
        
        localPrimaryY = 0;
        UIView *sepView = [UIView new];
        sepView.backgroundColor = COLOR_THEME;
        sepView.frame = CGRectMake(0, localPrimaryY, kWindowW, 5);
        [self addSubview:sepView];
        localPrimaryY += 5;
        
    }else{
        
        [self addSubview:self.topScrollView];
        localPrimaryY = HEIGHT_SCROLL_BAR;
        UIView *sepView = [UIView new];
        sepView.backgroundColor = COLOR_TOP_SEPERATOR_LINE;
        sepView.frame = CGRectMake(0, localPrimaryY, kWindowW, 1);
        sepView.layer.opacity = 0.3;
        [self addSubview:sepView];
        localPrimaryY += 1;
        
    }
    self.currentIndex = 0;
    
    self.bottomScrollView.frame = CGRectMake(0, localPrimaryY, kWindowW, self.needHeight - localPrimaryY);
    self.bottomScrollView.contentSize = CGSizeMake(self.tabTitles.count * kWindowW, 0);
    
    [self addSubview:self.bottomScrollView];
    
    self.frame = CGRectMake(0, primaryY, kWindowW, self.needHeight);
    [superView addSubview:self];
    
    return primaryY + self.needHeight;
    
}

#pragma mark UIScrollViewDelegate

-(void)scrollViewDidScroll:(UIScrollView *)scrollView{
    
    int index = scrollView.contentOffset.x / kWindowW;
    
    //位置没动 不变
    if (scrollView.contentOffset.x == self.previousOffsetX) {
        return;
    }
    
    //向左
    if (scrollView.contentOffset.x > self.previousOffsetX
        &&(scrollView.contentOffset.x != index * kWindowW)) {
        index += 1;
    }
    
    UIButton *button = [self.tabButtons objectAtIndex:index];
    
    [self topScroll:button];
    
}

-(void)topScroll:(UIButton *)button{
    
    if (self.isTopAnimating) {
        return;
    }
    
    if (self.selectedButton == button) {
        return;
    }
    
    NSInteger index = [self.tabButtons indexOfObject:button];
    self.currentIndex = (int)index;
    
    if (self.onTabIndexChangedHandler) {
        NSInteger index = [self.tabButtons indexOfObject:button];
        self.onTabIndexChangedHandler(index, [self.tabTitles objectAtIndex:index]);
        _currentTitle = [self.tabTitles objectAtIndex:index];
    }
    
    [self styleNormalButton:self.selectedButton];
    
    [self styleSelectedButton:button];
    
    self.selectedButton = button;
    
}

-(void)scrollViewDidEndDragging:(UIScrollView *)scrollView willDecelerate:(BOOL)decelerate{
    
    self.previousOffsetX = scrollView.contentOffset.x;
    
}

/**
 * 根据索引号获取索引所对应的父视图
 */
-(UIScrollView *)getSuperPanelByIndex:(NSInteger)index{
    
    if (self.scrollViews.count >= index + 1) {
        return [self.scrollViews objectAtIndex:index];
    }
    
    return nil;
}

/**
 * 根据标题获取标题所对应的父视图
 */
-(UIScrollView *)getSuperPanelByTitle:(NSString *)title{
    
    for (int i = 0; i < self.tabTitles.count; i++) {
        
        NSString *titleString = [self.tabTitles objectAtIndex:i];
        
        if ([title isEqualToString:titleString]) {
            return [self getSuperPanelByIndex:i];
        }
    }
    
    return nil;
}

-(void)buttonClick:(id)sender{
    
    UIButton *button = (UIButton *)sender;
    
    [self topScroll:button];
    
    NSInteger index = [self.tabButtons indexOfObject:button];
    
    [self.bottomScrollView setContentOffset:CGPointMake(index *kWindowW, 0) animated:YES];
    
}

-(NSMutableArray *)scrollViews{
    if (_scrollViews == nil) {
        _scrollViews = [NSMutableArray new];
        
        for (int i = 0; i < self.tabTitles.count; i++) {
            
            UIScrollView *scrollView = [UIScrollView new];
            scrollView.frame = CGRectMake(i * kWindowW, 0,
                                          kWindowW, self.bottomScrollView.frame.size.height);
            
            [_scrollViews addObject:scrollView];
            
            [self.bottomScrollView addSubview:scrollView];
            
        }
        
    }
    return _scrollViews;
}

-(UIScrollView *)topScrollView{
    if (_topScrollView == nil) {
        _topScrollView = [UIScrollView new];
        _topScrollView.frame = CGRectMake(0, 0, kWindowW, HEIGHT_SCROLL_BAR);
        if (self.tabBackgroundColor) {
            _topScrollView.backgroundColor = self.tabBackgroundColor;
        }
        
        CGFloat buttonWidth = kWindowW / self.tabTitles.count;
        
        self.tabButtons = [NSMutableArray new];
        
        for (int i = 0; i < self.tabTitles.count; i++) {
            
            NSString *buttonName = [self.tabTitles objectAtIndex:i];
            
            UIButton *tabButton = [UIButton new];
            [tabButton setTitle:buttonName forState:UIControlStateNormal];
            [self styleNormalButton:tabButton];
            [tabButton addTarget:self action:@selector(buttonClick:) forControlEvents:UIControlEventTouchUpInside];
            tabButton.frame = CGRectMake(buttonWidth * i, 0, buttonWidth, HEIGHT_SCROLL_BAR - HEGITH_SCROLL_LINEW);
            [_topScrollView addSubview:tabButton];
            [self.tabButtons addObject:tabButton];
            
            [self styleNormalButton:tabButton];
            
        }
        
        [self styleSelectedButton:[self.tabButtons objectAtIndex:0]];
        self.currentIndex = 0;
        self.selectedButton = [self.tabButtons objectAtIndex:0];
        
    }
    return _topScrollView;
}

-(void)styleNormalButton:(UIButton *)button{
    [button setTitleColor:self.textColor ? self.textColor : COLOR_TEXT_3 forState:UIControlStateNormal];
    button.titleLabel.font = self.normalTitleFont ? self.normalTitleFont : [UIFont systemFontOfSize:FONT_SIZE_3];
}

-(void)styleSelectedButton:(UIButton *)button{
    
    if (_selectedButton == button) {
        return;
    }
    
    [button setTitleColor:self.textColor ? self.textColor : COLOR_TEXT_1 forState:UIControlStateNormal];
    button.titleLabel.font = self.selectedTitleFont ? self.selectedTitleFont : [UIFont systemFontOfSize:14.0];
    
    CGFloat buttonWidth = kWindowW / self.tabTitles.count;
    
    [self.topScrollView addSubview:self.bottomLinewView];
    
    WS(weakSelf);
    
    CGRect buttonRect = button.frame;
    
    CGFloat margin = 15.0;
    
    self.isTopAnimating = YES;
    
    //滚动到指定位置
    
    [UIView animateWithDuration:0.2 animations:^{
        
        weakSelf.bottomLinewView.frame = CGRectMake(buttonRect.origin.x + (buttonWidth - 20.0)/2.0, HEIGHT_SCROLL_BAR - HEGITH_SCROLL_LINEW, 20.0, HEGITH_SCROLL_LINEW);
        
    } completion:^(BOOL finished) {
        dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.1 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
            weakSelf.isTopAnimating = NO;
        });
    }];
    
}

-(UIView *)bottomLinewView{
    if (_bottomLinewView == nil) {
        _bottomLinewView = [UIView new];
        if (self.tablineColor) {
            _bottomLinewView.backgroundColor = self.tablineColor;
        }else{
            _bottomLinewView.backgroundColor = COLOR_THEME;
        }
    }
    return _bottomLinewView;
}

-(UIScrollView *)bottomScrollView{
    if (_bottomScrollView == nil) {
        _bottomScrollView = [UIScrollView new];
        _bottomScrollView.bounces = NO;
        _bottomScrollView.showsVerticalScrollIndicator = NO;
        _bottomScrollView.showsHorizontalScrollIndicator = NO;
        _bottomScrollView.pagingEnabled = YES;
        _bottomScrollView.delegate = self;
    }
    return _bottomScrollView;
}

@end
